//
//  CNVisitorView.swift
//  Ccweibo
//
//  Created by 陈锦聪 on 16/3/20.
//  Copyright © 2016年 Coco. All rights reserved.
//

import UIKit

// MARK: - 代理协议
protocol CNVisitorViewDelegate: NSObjectProtocol {
    
    func visitorViewDidClickRegisterButton()
    
    func visitorViewDidClickLoginButton()
}

class CNVisitorView: UIView {
    
    weak var delegate: CNVisitorViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareSubView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupVisitorInfo(imageName: String, message: String) {
        
        sendSubviewToBack(coverView)
        homeView.hidden = true
        iconView.image = UIImage(named: imageName)
        msgLabel.text = message
    }
    
    // MARK: - 按钮点击事件
    func registerButtonAction() {
        
        delegate?.visitorViewDidClickRegisterButton()
    }
    
    func loginButtonAction() {
        
        delegate?.visitorViewDidClickLoginButton()
    }
    
    // MARK: - 旋转动画
    func startRotationAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.duration = 20;
        animation.toValue = M_PI * 2
        animation.repeatCount = MAXFLOAT
        // 不移除动画
        animation.removedOnCompletion = false
        iconView.layer.addAnimation(animation, forKey: "iconViewRotation")
    }
    
    // MARK: - 懒加载
    private lazy var iconView: UIImageView = {
        
        return UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    }()
    
    private lazy var coverView: UIImageView = {
        
        return UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    }()
    
    private lazy var homeView: UIImageView = {
        
        return UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    }()
    
    private lazy var msgLabel: UILabel = {
        
        let label = UILabel()
        label.text = "关注一些人,看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        label.sizeToFit()
        return label
    }()
    
    private lazy var registerBtn: UIButton = {
        
        let button = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.addTarget(self, action: "registerButtonAction", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    private lazy var loginBtn: UIButton = {
        
        let button = UIButton()
        button.setTitle("登陆", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.addTarget(self, action: "loginButtonAction", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    // MARK: - 布局子控件
    private func prepareSubView() {
        
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(msgLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        msgLabel.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

        
        addConstraint( NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: msgLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: msgLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 如果不参照其他控件:toItem为nil attribute为NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: msgLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: msgLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: msgLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: msgLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
    }
}

